var timer=require("../../utils/util.js");
var Que;
var page=0;
var kinds1 = [
  ["全部", "全部"],
  ["内科", "肝脏", "肾", "内分泌"],
  ["外科", "皮肤", "跌打损伤"],
  ["神经科", "多动症", "抽动症"]

]
var all=[];
var first="外科"
var temp1=[];
var temp2=[];
var on1=0;
var on2=0;
var bigfirst;
var smallfirst;
var rotate=1;
var wxNum;
var choose;
var thisKind;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Top:"Top",
    Bottom:'Bottom',
    padding:"",
    bigshow:[],
    smallshow:[],
    showkind1:"none",
    showkind2:"none",
    first:first,
    second:"",
    questions:"",
    kindsDisplay:"none",
    nowKind:"搜索分类",
    NoshowQue:'display',
    showQue:'none',
    full:'加载中...',
    hidden:true,
    chooseKind:[
      {
        kind:"全部",
        bgcolor:"whitesmoke",
      },
      {
        kind:"按个数",
        bgcolor:"white",
      },
      {
        kind:"按时间",
        bgcolor:"white"
      }
    ],
    kindLogoLeft:"0"
  },

  onPullDownRefresh: function () {
    this.questions();
   

  },
onReachBottom:function(){
  this.setData(
    {
      NoshowQue: 'display',
      showQue: 'none',
    }
  );
  this.addquestions();
},
addquestions:function(){
  var that = this;
  page++;
  wx.getStorage({
    key: 'login_uuId',
    success: function (res) {
      console.log(choose+"11111");
      wx.request({
        url: "https://www.liuxuanlong.xyz/fenda/wx/findByNotAnswerAndQuestionClass",
        data: {
          
          page: page,
          page_size: 10,
          choose:choose,   // 按照 时间 或者 回答个数 或者全部  
          openId: res.data,
          questionClass:thisKind
        },
        method: "GET",
        success: function (res) {
          page++;
          Que = res.data;
          var questions = res.data;
          var oldquestions=that.data.questions;
          var length=oldquestions.length;
          console.log(questions,'上拉加载的问题')
          var Time = Date.now();
          for (var i = 0; i < questions.length; i++) {
            if (questions[i].isAnonymous == 'true')
              questions[i].user.name = "匿名";
            questions[i].timeNum = (Time - questions[i].timeNum) / 60000;
            questions[i].timeNum = parseInt(questions[i].timeNum);
            if (questions[i].timeNum > 60) {
              if (questions[i].timeNum > 1440) {
                questions[i].timeNum = parseInt(questions[i].timeNum / 1440);
                questions[i].timeNum = questions[i].timeNum + "天";
              }
              else {
                questions[i].timeNum = parseInt(questions[i].timeNum / 60);
                questions[i].timeNum = questions[i].timeNum + '小时';
              }

            }
            else {
              questions[i].timeNum = questions[i].timeNum + '分钟'
            }
          }
          oldquestions=oldquestions.concat(questions);
          if(length==oldquestions.length){
            that.setData(
              {
                full:'到底了!'
              }
            );
          }
          that.setData(
            {
              questions: oldquestions,
              NoshowQue: 'none',
              showQue: 'display',
              hidden:false
            }
          );
          wx.stopPullDownRefresh();
          var t=setTimeout(that.loadingTo0,1000);
        },
      })
    },
  })
},
loadingTo0:function(){
  this.setData(
    {
      hidden:true,
      full:'加载中...'
    }
  );
},

/*Bottom:function(){
  
  this.tooltip();
  page++;
  var that=this;
  wx.getStorage({
    key: 'login_uuId',
    success: function (res) {
      wx.request({
        url: "https://www.liuxuanlong.xyz/fenda/wx/findByNotAnswerAndQuestionClass",
        data: {
          id: 2,
          page: page,
          page_size: 10,

          openId: res.data
        },
        method: "GET",
        success: function (res) {
          var oldquestions=that.data.questions;
          Que = res.data;
          var questions = res.data;
          var length=that.data.questions.length;
   
          var Time = Date.now();
          for (var i = 0; i < questions.length; i++) {
            questions[i].timeNum = (Time - questions[i].timeNum) / 60000;
            questions[i].timeNum = parseInt(questions[i].timeNum);
            if (questions[i].timeNum > 60) {
              if (questions[i].timeNum > 1440) {
                questions[i].timeNum = parseInt(questions[i].timeNum / 1440);
                questions[i].timeNum = questions[i].timeNum + "天";
              }
              else {
                questions[i].timeNum = parseInt(questions[i].timeNum / 60);
                questions[i].timeNum = questions[i].timeNum + '小时';
              }

            }
            else {
              questions[i].timeNum = questions[i].timeNum + '分钟'
            }
          }
          oldquestions=oldquestions.concat(questions);
         
          if(oldquestions.length==length){
            that.setData(
              {
                full:'到底了！',
                hidden:false

              }
            );
          }
          else{
            that.setData(
              {
                full:"加载中...",
                hidden:false
              }
            );
          }
          setTimeout(function(){
            that.setData(
              {
                hidden:true
              }
            );
          },1500);
        },
      })
    },
  })
},*/

 showKinds:function(){
   if(this.data.kindsDisplay=='none'){
   var allkind=this.data.kinds;
   var max=colors.length-10;
   for(var i=0;i<allkind.length;i++){
     var index=parseInt(Math.random()*10)+max;
     allkind[i].color=colors[index];
   }
   this.setData(
     {
       kindsDisplay:"display",
       kinds:allkind
     }
   );
   }
   else{
     this.setData(
       {
         kindsDisplay:"none"
       }
     );
   }

 },
 
 chooseKind:function(e){
   var index=e.currentTarget.id;
  
   wx.showToast({
     title: '加载中',
     icon: 'loading',
     duration: 800
   })
    this.setData(
      {
        kindLogoLeft:index*33.3
      }
    );
    choose=this.data.chooseKind[index].kind;

    this.questions();

   

 },
  toquestion:function(e){
    var id=this.data.questions[e.currentTarget.id].id;
   
    wx.navigateTo({
      url: '../question/question?id='+id,
    })
  },
  
  
  

  questions:function(){
    var that = this;
    console.log('分类',choose)
    wx.getStorage({
      key: 'login_uuId',
      success: function(res) {
        wx.request({
          url: "https://www.liuxuanlong.xyz/fenda/wx/findByNotAnswerAndQuestionClass",
          data: {
            id: 2,
            page: 0,
            page_size: 10,
            choose:choose,     //全部问题 还是  按时间 或者 按 回答个数
            openId:res.data,
            questionClass:thisKind
            
          },
          method: "GET",
          success: function (res) {
            page++;
            Que=res.data;
            var questions = res.data;
            console.log(questions,'加载的问题')
            var Time = Date.now();
            for (var i = 0; i < questions.length; i++) {
              if(questions[i].isAnonymous=='true')
              questions[i].user.name="匿名";
              questions[i].timeNum = (Time - questions[i].timeNum) / 60000;
              questions[i].timeNum = parseInt(questions[i].timeNum);
              
              if (questions[i].timeNum > 60) {
                if (questions[i].timeNum > 1440) {
                  questions[i].timeNum = parseInt(questions[i].timeNum / 1440);
                  questions[i].timeNum = questions[i].timeNum + "天";
                }
                else {
                  questions[i].timeNum = parseInt(questions[i].timeNum / 60);
                  questions[i].timeNum = questions[i].timeNum + '小时';
                }

              }
              else {
                questions[i].timeNum = questions[i].timeNum + '分钟'
              }
            }
         
            that.setData(
              {
                questions: questions,
                NoshowQue: 'none',
                showQue: 'display'
              }
            );
            wx.stopPullDownRefresh();
          },
        })
      },
    })
   
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  Login: function () {
    var that = this;

    wx.checkSession({
      success: function () {
        wx.getStorage({
          key: 'login_uuId',
          success: function (res) {
          
            wxNum = res.data;
            console.log(wxNum,'登录得到的openid');
            wx.request({
              url: "https://www.liuxuanlong.xyz/fenda/wx/checkLoginTime",
              data: {
                login_uuId: wxNum
              },
              success: function (res) {
                if (res.data == 'error') {
                  that.tologin();
                  
                }
                else {
                  that.questions();
                  //that.CheckMaster();
                }
              },

            })

          },
        })

      },
      fail: function () {
     
        that.tologin();
      },

    })


    //

  },
  loadKind:function(){
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/findHospitalField",
      data:{
        hospitalId: getApp().globalData.hospital_id
      },
      success:function(res){
        console.log(res.data,'医院的分类加载');
      },
    })
  },
CheckMaster:function(){
 var that=this;
  wx.request({
    url: "https://www.liuxuanlong.xyz/fenda/wx/checkMaster",
    data: {
      wxNum: wxNum
    },
    success: function (res) {
     
      if (res.data == 'no') {
        wx.redirectTo({
          url: '../login/login',
        })
        
       
      }
      else {
        //that.loadquestion();
        that.questions();
        //that.loadKind();
        
      }
    },
  })
},

tologin: function () {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.liuxuanlong.xyz/fenda/wx/wxLoginMaster',
            data: {
              code: res.code
            },
            success: function (res) {
              
              wx.setStorage({
                key: "login_uuId",
                data: res.data
              });
              wxNum = res.data;
              //that.CheckMaster();
              that.questions();
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.avatarUrl)
        }
      }
    })
  },
 
  onLoad: function (options) {
    //this.loadquestion();
  
    thisKind=options.kind;
   
    choose=this.data.chooseKind[0].kind;
    this.Login();
    
    
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //this.loadquestion();
    this.questions();
    this.loadKind();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  
  /**
   * 页面上拉触底事件的处理函数
   */
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})